package me.zhengjie.modules.security.service;

import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.security.security.vo.JwtUser;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.DeptSmallDto;
import me.zhengjie.modules.system.service.dto.JobSmallDto;
import me.zhengjie.modules.system.service.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * Description: UserDetailsServiceImpl .<br>
 *
 * @author jm
 * @since 2020/1/20
 */
@Service("userDetailsService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired private UserService userService;
    @Autowired private RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        UserDto user = userService.findByName(username);
        if (user == null) {
            throw new BadRequestException("账号不存在");
        } else {
            if (!user.getEnabled()) {
                throw new BadRequestException("账号未激活");
            }
            return createJwtUser(user);
        }
    }

    private UserDetails createJwtUser(UserDto user) {
        return new JwtUser(
            user.getId(),
            user.getUsername(),
            user.getNickName(),
            user.getSex(),
            user.getPassword(),
            user.getAvatar(),
            user.getEmail(),
            user.getPhone(),
            Optional.ofNullable(user.getDept()).map(DeptSmallDto::getName).orElse(null),
            Optional.ofNullable(user.getJob()).map(JobSmallDto::getName).orElse(null),
            roleService.mapToGrantedAuthorities(user),
            user.getEnabled(),
            user.getLastPasswordResetTime(),
            user.getCreateTime()
        );
    }
}
